package com.gdth.bank.presenter;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import com.gdth.bank.model.CjkhBfrjModel;
import com.gdth.bank.view.CjkhBfrjView;
import com.gdth.database.bean.TAppBfrjBfjl;

public class CjkhBfrjPresenter {
	private CjkhBfrjView mCjkhBfrjView;
	private CjkhBfrjModel mCjkhBfrjModel;
	
	public CjkhBfrjPresenter(CjkhBfrjView mCjkhBfrjView) {
		this.mCjkhBfrjView = mCjkhBfrjView;
		this.mCjkhBfrjModel = new CjkhBfrjModel();
	}
	
	public void getBfjlByUuid(String jcuuid){
		mCjkhBfrjModel.getBfjlByUuid(jcuuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<TAppBfrjBfjl>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mCjkhBfrjView.showMessage("获取失败");
			}

			@Override
			public void onNext(List<TAppBfrjBfjl> arg0) {
				// TODO Auto-generated method stub
				mCjkhBfrjView.getAllBrjl(arg0);
			}
		});
	}
	
	public void save(TAppBfrjBfjl mTAppBfrjBfjl, final int type) {
		mCjkhBfrjView.showProgressDialog("正在保存...");
		
		mCjkhBfrjModel.saveCjkhjbxx(mTAppBfrjBfjl)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<Long>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					mCjkhBfrjView.hideProgressDialog();
					mCjkhBfrjView.showMessage("保存成功");
				}

				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					mCjkhBfrjView.hideProgressDialog();
					mCjkhBfrjView.showMessage(arg0.getMessage());
				}

				@Override
				public void onNext(Long arg0) {
					// TODO Auto-generated method stub
					mCjkhBfrjView.saveSuccess(arg0, type);
				}
				
			});
	}

	public void DelBfjl(TAppBfrjBfjl mTAppBfrjBfjl) {
		mCjkhBfrjModel.deletebfjl(mTAppBfrjBfjl)
				.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Subscriber<Long>(){

					@Override
					public void onCompleted() {
					}

					@Override
					public void onError(Throwable arg0) {
					}

					@Override
					public void onNext(Long arg0) {

						mCjkhBfrjView.DelBfjl();
					}

				});
	}
}
